/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-10-14
 * Time: 22:13
 */
public class Solution {

    // 滑动窗口
    public static int equalSubstring(String s, String t, int maxCost) {
        int maxLen = 0;
        int sum = 0;
        int left = 0;
        int right = 0;
        int len = s.length();
        while (right < len) {
            sum += Math.abs(s.charAt(right) - t.charAt(right));
            // 如果大于要求的值，移动左指针
            if (sum > maxCost) {
                // 更新最大值
                maxLen = Math.max(maxLen, right-left);
                while (sum > maxCost && left <= right) {
                    sum -= Math.abs(s.charAt(left) - t.charAt(left));
                    left++;
                }
            }
            right++;
        }
        // 最后不要忘了更新一下最大值
        maxLen = Math.max(maxLen, right-left);
        return maxLen;
    }
}
